import React, { Component } from 'react';
import { Form, Input, Select, message } from 'antd';
import Model from '@/models/user'
const layout = {
    labelCol: {
        span: 8,
    },
    wrapperCol: {
        span: 16,
    },
};
class UpdateUser extends Component {
    state = {
        alldata: [],
    }
    render() {
        let { sentrole, sentupInfo } = this.props
        return (
            <>
                <Form
                    {...layout}
                    name="updateuser"
                    initialValues={{
                        remember: true,
                    }, sentupInfo}
                    onFieldsChange={this.getallfields}
                >
                    <Form.Item
                        name="username"
                        label="用户名"
                        tooltip="至少输入五个无限制字符！"
                        rules={[
                            {
                                required: true,
                                message: '请输入你的昵称',
                                whitespace: true,
                            },
                            ,
                            {
                                pattern: /^.{5,}$/,
                                message: "格式不正确！"
                            }
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        name="password"
                        label="密码"
                        tooltip="给爷填"
                        rules={[
                            {
                                required: true,
                                message: '请输入你的密码',
                                whitespace: true,
                            },
                        ]}
                    >
                        <Input.Password/>
                    </Form.Item>
                    <Form.Item
                        name="phone"
                        label="手机号"
                        tooltip="手机号11位"
                        rules={[
                            {
                                required: true,
                                message: '请输入你的手机号',
                                whitespace: true,
                            },
                            {
                                pattern: /^1[3-8]{1}\d{9}$/,
                                message: "格式不正确！"
                            }
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        name="email"
                        label="邮箱"
                        tooltip="请输入你的邮箱！"
                        rules={[
                            {
                                required: true,
                                message: '请输入你的密码',
                                whitespace: true,
                            },
                            {
                                pattern: /^[^_$].{6,}@(163|126|qq|sina|QQ|gmail|sohu|yahoo|hotmail)\.(com|cn|net|COM|CN|NET)$/,
                                message: "格式不正确！"
                            }
                        ]}
                    >
                        <Input />
                    </Form.Item>
                    <Form.Item
                        name="role_id"
                        label="角色"
                        rules={[
                            {
                                required: true,
                                message: '请选择角色！',
                            },
                        ]}
                    >
                        <Select placeholder="请选择角色" >
                            {
                                sentrole.map(el => {
                                    return (
                                        <Select.Option key={el._id} value={el._id}>{el.name}</Select.Option>
                                    )
                                })
                            }
                        </Select>
                    </Form.Item>
                </Form>
            </>
        );
    }
    componentDidUpdate() {
        if (this.props.submitadd2) {
            let { alldata } = this.state
            // console.log(this.state.alldata);
            let values = {}
            values['username'] = alldata[0] ? alldata[0] : this.props.sentupInfo.username
            values['password'] = alldata[1] ? alldata[1] : this.props.sentupInfo.password
            values['phone'] = alldata[2] ? alldata[2] : this.props.sentupInfo.phone
            values['email'] = alldata[3] ? alldata[3] : this.props.sentupInfo.email
            values['role_id'] = alldata[4] ? alldata[4] : this.props.sentupInfo.role_id
            values['_id'] = this.props.sentupInfo._id
            // console.log(values);
            // console.log('提交表单信息');
            Model.updateUser(values).then(ret => {
                // console.log(ret.data);
                if (ret.status === 0)
                    message.success("修改成功！")
                else
                    message.error("修改失败！")
            })
            return true
        }
        else
            return false
    }
    getallfields = (c, all) => {
        // console.log(all.length);
        let alldata = []
        for (var i = 0; i < all.length; i++)
            alldata.push(all[i].value)
        // console.log(alldata);
        this.setState({
            alldata
        })
    }
}

export default UpdateUser;
